package com.enwei.activitys;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.enwei.adapters.BBSListAdapter;
import com.enwei.base.BaseActivity;
import com.enwei.utils.Constans;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.enwei.views.popwindow.MorePopWindow;
import com.enwei.views.popwindow.MorePopWindow.PopListener;
import com.ew.action.R;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class BBSInfoListActivity extends BaseActivity{
	
	private ListView mListView;
	private BBSListAdapter mAdapter;
	private ImageButton button;
	private Button mAdd;
	private JSONArray arrays = new JSONArray();
	private MorePopWindow morePopWindow = null;
	private int typeId = -1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bbs_info_list,true,true);
		mListView = (ListView) findViewById(R.id.ordersList);
		button = (ImageButton) findViewById(R.id.button);
		mAdd = (Button) findViewById(R.id.add);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(morePopWindow != null){
					morePopWindow.showPopupWindow(button);
				}
			}
		});
		mAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(BBSInfoListActivity.this, BBSAddActivity.class);
				intent.putExtra(BBSAddActivity.BBS_TIEZI_ID, typeId);
				startActivityForResult(intent, 200);
			}
		});
		initData();
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				try {
					JSONObject jsonObj = arrays.getJSONObject(arg2);
					Intent intent = new Intent(BBSInfoListActivity.this, BBSDetailActivity.class);
					intent.putExtra(BBSDetailActivity.BBS_TIEZI_ID, jsonObj.getInt("tieid"));
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void initData(){
		mAdapter = new BBSListAdapter(arrays, this);
		mListView.setAdapter(mAdapter);
		requestData();
	}
	
	public void requestData(){
		String url = Constans.CK_TIEZI;
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		if(typeId != -1){
			nameValuePairs.add(new BasicNameValuePair("bkid", typeId+""));
		}
		HttpApi http = new HttpApi(this,HttpMethod.POST);
		http.sendHttpRequest(url, nameValuePairs, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				try {
					getLoadingView().setVisibility(View.GONE);
					if(!TextUtils.isEmpty(data)){
						JSONObject jsonObj = new JSONObject(data);
						morePopWindow = new MorePopWindow(BBSInfoListActivity.this,new JSONArray(jsonObj.getString("fenlei")));
						morePopWindow.setmClickListener(new PopListener() {
							
							@Override
							public void onClickListener(JSONObject jsonObj) {
								try {
									setTitle(jsonObj.getString("title"));
									typeId = jsonObj.getInt("id");
									morePopWindow.dismiss();
									
									mListView.setVisibility(View.GONE);
									getLoadingView().setVisibility(View.VISIBLE);
									getErrorLayout().setVisibility(View.GONE);
									requestData();
									
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
						arrays = new JSONArray(jsonObj.getString("tz")) ;
						mAdapter.updateView(arrays);
						mListView.setVisibility(View.VISIBLE);
					}else{
						getEmpty().setVisibility(View.VISIBLE);
					}
				} catch (JSONException e) {
					getErrorLayout().setVisibility(View.VISIBLE);
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure() {
				getLoadingView().setVisibility(View.GONE);
				getErrorLayout().setVisibility(View.VISIBLE);
				super.onFailure();
			}
		});
	}
	
	@Override
	protected void onRetry() {
		mListView.setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		getErrorLayout().setVisibility(View.GONE);
		requestData();
		super.onRetry();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 200 && resultCode == RESULT_OK){
			requestData();
		}
	}
}